package com.wnxy.pawnbackgroundsystem.commons;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wnxy.pawnbackgroundsystem.entity.QQMessage;
import com.wnxy.pawnbackgroundsystem.exception.DemoRuntimeException;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

//获取qq信息
public class QQMessageUtil {
    private static RestTemplate restTemplate = new RestTemplate();
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static QQMessage getQQMessage(String code){
        //第二步,通过code获取access token
        String url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101780702&client_secret=be3cf79b89eafe3f0b8f90462aed8065&code="+code+"&redirect_uri=http://www.pawntest.com/qqlogin";
        String value = restTemplate.getForObject(url, String.class);
        String token = value.substring(value.indexOf("=") + 1, value.indexOf("&"));
        //第三步，获取OpenId
        url = "https://graph.qq.com/oauth2.0/me?access_token="+token;
        value = restTemplate.getForObject(url,String.class);
        String openId = value.substring(value.lastIndexOf(":")+2,value.lastIndexOf("\""));
        System.out.println(value+"\n"+openId);
        //第四部，获取信息
        url = "https://graph.qq.com/user/get_user_info?access_token="+token+"&oauth_consumer_key=101780702&openid="+openId;
        value = restTemplate.getForObject(url,String.class);
        System.out.println(value);
        QQMessage qqMessage = null;
        try {
           qqMessage = objectMapper.readValue(value, QQMessage.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        if (null == qqMessage){
            throw new DemoRuntimeException("4001","QQ号未注册");
        }
        qqMessage.setOpenId(openId);
        return qqMessage;
    }
    public static final String QQ_LOGIN_SUCCESS = "登陆成功";
    public static final String QQ_LOGIN_FAIL = "登陆失败";
    public static String qqWindon(){
        return qqWindon(QQ_LOGIN_FAIL,"undefined");
    }
    public static <T> String qqWindon(String message ,T data) {
        String json = null;
        try {
            json = objectMapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        json = null != json?json:"undefined";
        return qqWindon(message,json);
    }
    public static String qqWindon(String message ,String json){
        StringBuffer sbs = new StringBuffer();
        sbs.append("<!DOCTYPE html>");
        sbs.append("<html><head><meta charset='utf-8'><title></title></head><body>");
        sbs.append("<h1 style='text-align:center;padding-top: 120px'>"+message+"</h1>");
        //是通过js向父窗口发送一个消息数据，登陆成功相应的是帐号信息对象的json字符串
        sbs.append("<script>window.opener.postMessage('"+json+"','*')</script>");
        sbs.append("</body></html>");
        return sbs.toString();
    }
}
